package ServerApp;

import java.io.File;
import javax.swing.JOptionPane;
import javax.xml.parsers.*;
import org.w3c.dom.*;

/**
 * Contains some helpful methods
 * @author Matthias Olbinski
 * test
 */
public class Tools {

	/**
	 * Displays a message window
	 * @param Title Title of the window
	 * @param Text Content of the window
	 */
	public static void message(String Title, String Text){
		JOptionPane.showMessageDialog(null,Text,Title,JOptionPane.INFORMATION_MESSAGE);
	}
	
	/**
	 * Displays an error message window
	 * @param Title Title of the window
	 * @param Text Content of the window
	 */
	public static void errorMessage(String Title, String Text){
		JOptionPane.showMessageDialog(null, Text, Title, JOptionPane.ERROR_MESSAGE);
	}
	
	/**
	 * Displays an query message window
	 * @param Title Title of the window
	 * @param Text Content of the window
	 * @return an integer indicating the option chosen by the user 
	 */
	public static int questionYNC(String Title, String Text){
		Object[] options = {"Yes","No","Cancel"};
		int res = JOptionPane.showOptionDialog(null, Text, Title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
		return res;
	}
	
	/**
	 * Centers a given Frame on the screen.
	 * @param frame Java Swing object to be centered
	 * @param preset Preset for the size of the window
	 */
	public static void centerFrameOnScreen(java.awt.Window frame, java.awt.Dimension preset) {
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        if (preset == null)
            preset = frame.getSize();
        if (preset.height > screenSize.height)
            preset.height = screenSize.height;
        if (preset.width > screenSize.width)
            preset.width = screenSize.width;
        // set new pos:
        frame.setBounds((screenSize.width - preset.width) >>1, (screenSize.height - preset.height) >> 1,
            preset.width, preset.height);
    }
	
	/**
	 * Reads the contents of the input file and creates a xml document object
	 * @param file Input file
	 * @return xml document
	 */
	public static Document getXMLDocument(File file)
	{
		try
		{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(file);
			document.getDocumentElement().normalize();
			return document;
		}
		catch (Exception ex)
		{
			return null;
		}
	}
}
